package com.lagou.edu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;

/**
 * @author renpeiqian
 * 处理用户名和密码等事宜
 * @date 2021/7/20 15:34
 * @since
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * @Description : 添加一个认证管理器到容器
     * @Param []
     * @return org.springframework.security.authentication.AuthenticationManager
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * @Description ：注入密码编码器（用加密算法（MD5）等对密码进行加密）
     *  此处为了测试对密码暂时不不进行加密处理
     * @Param 1
     * @return org.springframework.security.crypto.password.PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * @Description：校验用户名和密码
     * 1.客户端传递用户名username和密码password到认证服务器
     * 2.username和password 一般存储在数据库中的用户表中
     * 3.根据用户表中数据，验证当前传递的参数准确性
     * @Param [auth]
     * @return void
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        //模拟username、password校验
        UserDetails user = new User("admin","123456",new ArrayList<>());
        //将用户信息读取到内存中，为后续校验做准备。
        auth.inMemoryAuthentication()
                .withUser(user)
                //配置密码编码器（用户密码加密）
                .passwordEncoder(passwordEncoder);
    }




}
